import cv2
import numpy as np

from config_logger import log


def is_black_frame(frame, threshold=10):
    gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    mean_brightness = np.mean(gray_frame)

    return mean_brightness < threshold


def detect_black_frames(video_path, threshold=10):
    cap = cv2.VideoCapture(video_path)

    frame_count = 0
    black_frame_indices = []

    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break

        frame_count += 1

        if is_black_frame(frame, threshold):
            black_frame_indices.append(frame_count)

    cap.release()

    log.info(black_frame_indices)

    if len(black_frame_indices) > 5:
        return len(black_frame_indices)
    else:
        return False


if __name__ == '__main__':
    print(detect_black_frames(r"D:\剪映\导出\372836846721308396.mp4"))
